﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;
using NLBS.Data.Business;
using NLBS.Resources;

namespace NLBS.Views
{

    public interface ITrackBackView:IErrorView
    {
        List<TrackBack> tbList { get; set; }

        int catID { get; set; }

        int logID { get; set; }

        string pageUrl { get; set; }

        string q { get; }

        int startIndex { get; set; }
        int pageSize { get; set; }

    }
   public class TrackBackPresenter:Presenter<ITrackBackView,IAction<TrackBack>>
    {
        private User currentUser = BlogInfo.Instance().CurrentUser;
        private Right power = BlogInfo.Instance().CurrentUser.Group.Power;
        private Dictionary<string, string> lang = Language.Instance().getLanguage();
       public override void OnViewInitialized()
       {
           
           base.OnViewInitialized();
           DataList();
       }

       public override void OnActionLoaded()
       {
           this.Action.DataDelete += new EventHandler(Action_DataDelete);
           base.OnActionLoaded();
       }

       void Action_DataDelete(object sender, EventArgs e)
       {
           TrackBackData.DeleteTrackBack(this.Action.keyID);
       }

       private void DataList()
       {
           List<TrackBack> lists = TrackBackData.GetTrackBack();
           if (!string.IsNullOrEmpty(View.q))
           {
               List<TrackBack> searchList = lists.FindAll(delegate(TrackBack t)
               {
                   return t.Title.Contains(View.q);
               });
               View.tbList = searchList;
               View.pageUrl = "javascript:NLBSPage.TrackBackCall('{0}','q','" + View.q + "')";
               
               return;
           }


           
           //过滤日志分类引用
           if (View.catID > 0)
           {
               lists = lists.FindAll(delegate(TrackBack tb)
               {
                   return tb.Article.CategoryID == View.catID;
               });
           }
           //过滤日志引用
           if (View.logID > 0)
           {
               lists = lists.FindAll(delegate(TrackBack tb)
               {
                   return tb.LogID == View.logID;
               });
           }
           View.tbList = lists;
       }

    }
}
